//
//  XJZPQSearchShopViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  宅配圈搜索店铺

#import "XJZPQSearchShopViewController.h"
#import "XJShopDetailViewController.h"

#import "XJSearchShopModel.h"
#import "XJSearchShopsCell.h"


@interface XJZPQSearchShopViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *shopsArray;

@end

@implementation XJZPQSearchShopViewController

#pragma mark - 一 lazy
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        NSString *classStr = NSStringFromClass([XJSearchShopsCell class]);
        
        [_collectionView registerNib:[UINib nibWithNibName:classStr bundle:nil] forCellWithReuseIdentifier:classStr];
    }
    return _collectionView;
}

- (NSMutableArray *)shopsArray {
    if (!_shopsArray) {
        _shopsArray = [NSMutableArray array];
    }
    return _shopsArray;
}
#pragma mark - 二 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor greenColor];

    self.collectionView.frame = self.view.bounds;
    [self.view addSubview:self.collectionView];
}


#pragma mark -  三 UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return 30;
   return self.shopsArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *classStr = NSStringFromClass([XJSearchShopsCell class]);

    XJSearchShopsCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:classStr forIndexPath:indexPath];
    cell.searchModel = self.shopsArray[indexPath.row];
    
    
    return cell;
}

#pragma mark - 三-2 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KWidth-4)/2, 200);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}

#pragma mark -  三 -3 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XJSearchShopModel *model = self.shopsArray[indexPath.row];
    XJLog(@"%@",model);
    
    
    XJShopDetailViewController *shopDetailVC = [[XJShopDetailViewController alloc] init];
    shopDetailVC.shopId = model.shopId;
    
    [self.navigationController pushViewController:shopDetailVC animated:YES];
}


#pragma mark - 网络请求
- (void)requestDataWithKeyWord:(NSString *)keyword {
    
    //    NSString *lat = @"30.194859";
    //    NSString *lng = @"120.189506";
    
    [XJHttpTool requestHomeSearchWithType:@"1" keyWord:keyword lat:@"30.194859" lng:@"120.189506" page:@"1" successH:^(id responseObjc) {
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        XJLog(@"%@",jsonStr);
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSArray *shops = responseObjc[@"data"][@"shops"];
            
            if (shops.count == 0) {
                
                [JRToast showWithText:@"没有数据!"];
                return ;
            }
            
            for (NSDictionary *tempDic in shops) {
                XJSearchShopModel *model = [XJSearchShopModel mj_objectWithKeyValues:tempDic];
                
                [self.shopsArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
            
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
        
    } failH:^(NSError *error) {
        XJLog(@"失败:%@",error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
